# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ghostscript

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=9.14
pkgrel=1
arch=('any')
groups=("${MINGW_PACKAGE_PREFIX}")
license=('AGPL' 'custom')
pkgdesc="An interpreter for the PostScript language (mingw-w64)"
depends=(
    "${MINGW_PACKAGE_PREFIX}-freetype"
    "${MINGW_PACKAGE_PREFIX}-fontconfig"
    "${MINGW_PACKAGE_PREFIX}-jasper"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-libpng>=1.5.7"
    "${MINGW_PACKAGE_PREFIX}-libjpeg"
    "${MINGW_PACKAGE_PREFIX}-libtiff>=4.0.0"
    "${MINGW_PACKAGE_PREFIX}-lcms2"
    "${MINGW_PACKAGE_PREFIX}-dbus"
    #"${MINGW_PACKAGE_PREFIX}-openjpeg"
    "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
    )
makedepends=("${MINGW_PACKAGE_PREFIX}-gtk3" "${MINGW_PACKAGE_PREFIX}-gnutls")
optdepends=("${MINGW_PACKAGE_PREFIX}-gtk3:          needed for gsx")
url="http://www.ghostscript.com/"
source=(http://downloads.ghostscript.com/public/ghostscript-${pkgver}.tar.bz2
    mingw-build.patch
    ghostscript-sys-zlib.patch)
options=('staticlibs' 'strip')
md5sums=('75f2e8ab8891d052ade9b64eb4eb5294'
         '80ef59aa3682b3b53b7b8f52ee862d78'
         '24169cd027208610fd40070bae68ca73')

prepare() {
  cd "$srcdir/${_realname}-$pkgver"
  patch -p1 -i ${srcdir}/mingw-build.patch
  patch -p1 -i ${srcdir}/ghostscript-sys-zlib.patch

  sed -e "s|/usr|${MINGW_PREFIX}/${MINGW_CHOST}|g" -i base/all-arch.mak base/unix-aux.mak
  
  # force it to use system-libs
  rm -rf freetype jpeg libpng lcms lcms2 tiff zlib #openjpeg

  #cd base
  libtoolize --force --copy --install
  autoconf -f -i
}

build()
{
  cd ${srcdir}/${_realname}-$pkgver
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --with-drivers=ALL \
    --disable-cups \
    --with-system-libtiff \
    --with-jbig2dec \
    --enable-openjpeg \
    --enable-fontconfig \
    --enable-freetype \
    --disable-contrib \
    --without-x
    
  make so
}

package() {
  cd ${srcdir}/${_realname}-$pkgver
  make DESTDIR="$pkgdir" soinstall

  # install missing doc files # http://bugs.archlinux.org/task/18023
  install -m 644 "${srcdir}"/${_realname}-${pkgver}/doc/{Ps2ps2.htm,gs-vms.hlp,gsdoc.el,pscet_status.txt} "${pkgdir}${MINGW_PREFIX}"/share/${_realname}/$pkgver/doc/

  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE

  # remove unwanted localized man-pages
  rm -rf "${pkgdir}${MINGW_PREFIX}"/share/man/[^man1]*
}
